﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace UtilLib
{
    /// <summary>
    /// 扩展
    /// </summary>
    public static partial class Util
    {
        /// <summary>
        /// 移除成员
        /// </summary>
        /// <typeparam name="T1"></typeparam>
        /// <typeparam name="T2"></typeparam>
        /// <param name="source"></param>
        /// <param name="collection"></param>
        public static void RemoveAll<T1, T2>(this Dictionary<T1, T2> source, IEnumerable<T1> collection)
        {
            foreach (var item in collection)
            {
                source.Remove(item);
            }
        }
        /// <summary>
        /// 移除成员
        /// </summary>
        /// <typeparam name="T1"></typeparam>
        /// <typeparam name="T2"></typeparam>
        /// <param name="source"></param>
        /// <param name="predicate"></param>
        public static void RemoveAll<T1, T2>(this Dictionary<T1, T2> source, Func<KeyValuePair<T1, T2>, bool> predicate)
        {
            var sourceselect = source.Where(predicate);
            foreach (var item in sourceselect)
            {
                source.Remove(item.Key);
            }
        }
        /// <summary>
        /// 移除成员
        /// </summary>
        /// <typeparam name="T1"></typeparam>
        /// <typeparam name="T2"></typeparam>
        /// <param name="source"></param>
        /// <param name="collection"></param>
        public static void RemoveAll<T1, T2>(this SortedDictionary<T1, T2> source, IEnumerable<T1> collection)
        {
            foreach (var item in collection)
            {
                source.Remove(item);
            }
        }
        /// <summary>
        /// 移除成员
        /// </summary>
        /// <typeparam name="T1"></typeparam>
        /// <typeparam name="T2"></typeparam>
        /// <param name="source"></param>
        /// <param name="predicate"></param>
        public static void RemoveAll<T1, T2>(this SortedDictionary<T1, T2> source, Func<KeyValuePair<T1, T2>, bool> predicate)
        {
            var sourceselect = source.Where(predicate);
            foreach (var item in sourceselect)
            {
                source.Remove(item.Key);
            }
        }
        /// <summary>
        /// 字典加值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="source"></param>
        /// <param name="k"></param>
        /// <param name="v"></param>
        public static void AddValue<T>(this Dictionary<T, int> source, T k, int v = 1)
        {
            var old = source.GetValueOrDefault(k);
            source[k] = old + v;
        }
    }
}
